 

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SpaceWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
    private static boolean menuRemoved = false;
    private GreenfootImage MouseImage;
    private menu menu;
    private Counter counter;
    private Level level=null;
    private int timer=0;
    private int speedGen = 80;
    
    public SpaceWorld()
    {    
        super(480, 600, 1);

        MouseImage=new GreenfootImage(32,32);
        MouseImage.fillOval(0,0,1,1);

        menu = new menu();
        addObject(menu, 309, 308);
        menu.setLocation(242, 300);
    }

    public void act()
    {
        if(menuRemoved==false) {prepare(); menuRemoved=true;}
        Mouse.setImage(MouseImage,0,0);
        if(Greenfoot.isKeyDown("escape")) {  Greenfoot.stop();  }
       
        timer++;
        if (timer >= speedGen)
        {
           timer=0;
           addObject(new Asteroid(counter), Greenfoot.getRandomNumber(getWidth()+1), 2);
        }
        
        if(level!=null) speedGen = 80/level.getLevel();
       
        
    }
    
    private void prepare()
    {
        removeObject(menu);

        counter = new Counter();
        addObject(counter, 503, 42);
        counter.setLocation(435, 16);
        
        level = new Level();
        addObject(level, 200, 30);
        level.setLocation(100, 5);

        Defender defender = new Defender(this,counter,level);
        addObject(defender, 300, 355);
        defender.setLocation(236, 571);
    }
}
